<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="死锁的基本概念死锁与饥饿 死锁：一组处于等待（阻塞）状态的进程，每一个进程持有其他进程所需要的资源，而又等待使用其他进程所拥有的资源，致使这组进程互相等待，均无法向前推进。另一种定义：当一组进程中每个进程都在等待一个事件，而这一事件只能由这一组进程的另一个进程引起时，这组进程处于死锁状态。 饥饿：就绪进程长时间得不到调度是处于等待状态，而不是死锁中的互相等待。若信号量的等待队列按照LIFO或优先级">
<meta property="og:type" content="article">
<meta property="og:title" content="死锁">
<meta property="og:url" content="http://example.com/2022/03/20/%E6%AD%BB%E9%94%81/index.html">
<meta property="og:site_name" content="JsyBlog">
<meta property="og:description" content="死锁的基本概念死锁与饥饿 死锁：一组处于等待（阻塞）状态的进程，每一个进程持有其他进程所需要的资源，而又等待使用其他进程所拥有的资源，致使这组进程互相等待，均无法向前推进。另一种定义：当一组进程中每个进程都在等待一个事件，而这一事件只能由这一组进程的另一个进程引起时，这组进程处于死锁状态。 饥饿：就绪进程长时间得不到调度是处于等待状态，而不是死锁中的互相等待。若信号量的等待队列按照LIFO或优先级">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/2022/03/20/%E6%AD%BB%E9%94%81/rag.png">
<meta property="article:published_time" content="2022-03-20T07:36:12.000Z">
<meta property="article:modified_time" content="2022-09-22T07:44:56.296Z">
<meta property="article:author" content="SongyangJi">
<meta property="article:tag" content="OS">
<meta property="article:tag" content="算法">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/2022/03/20/%E6%AD%BB%E9%94%81/rag.png">


<link rel="canonical" href="http://example.com/2022/03/20/%E6%AD%BB%E9%94%81/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2022/03/20/%E6%AD%BB%E9%94%81/","path":"2022/03/20/死锁/","title":"死锁"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>死锁 | JsyBlog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">JsyBlog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%AD%BB%E9%94%81%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="nav-number">1.</span> <span class="nav-text">死锁的基本概念</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%AD%BB%E9%94%81%E4%B8%8E%E9%A5%A5%E9%A5%BF"><span class="nav-number">1.1.</span> <span class="nav-text">死锁与饥饿</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%AD%BB%E9%94%81%E5%BD%A2%E6%88%90%E7%9A%84%E6%9D%A1%E4%BB%B6"><span class="nav-number">1.2.</span> <span class="nav-text">死锁形成的条件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%AD%BB%E9%94%81%E7%9A%84%E5%A4%84%E7%90%86%E6%96%B9%E6%B3%95"><span class="nav-number">1.3.</span> <span class="nav-text">死锁的处理方法</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%AD%BB%E9%94%81%E9%A2%84%E9%98%B2"><span class="nav-number">2.</span> <span class="nav-text">死锁预防</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E4%BA%92%E6%96%A5"><span class="nav-number">2.0.1.</span> <span class="nav-text">1. 互斥</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E5%8D%A0%E6%9C%89%E5%B9%B6%E7%AD%89%E5%BE%85"><span class="nav-number">2.0.2.</span> <span class="nav-text">2. 占有并等待</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E9%9D%9E%E6%8A%A2%E5%8D%A0"><span class="nav-number">2.0.3.</span> <span class="nav-text">3. 非抢占</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E5%BE%AA%E7%8E%AF%E7%AD%89%E5%BE%85"><span class="nav-number">2.0.4.</span> <span class="nav-text">4. 循环等待</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%AD%BB%E9%94%81%E9%81%BF%E5%85%8D"><span class="nav-number">3.</span> <span class="nav-text">死锁避免</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%89%E5%85%A8%E7%8A%B6%E6%80%81-safe-state"><span class="nav-number">3.1.</span> <span class="nav-text">安全状态(safe state)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8D%95%E5%AE%9E%E4%BE%8B%E2%80%94%E2%80%94%E8%B5%84%E6%BA%90%E5%88%86%E9%85%8D%E5%9B%BE-RAG%E2%80%94%E2%80%94resource-allocation-graph"><span class="nav-number">3.2.</span> <span class="nav-text">单实例——资源分配图(RAG——resource-allocation graph)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A4%9A%E5%AE%9E%E4%BE%8B%E2%80%94%E2%80%94%E9%93%B6%E8%A1%8C%E5%AE%B6%E7%AE%97%E6%B3%95-Banker%E2%80%99s-Algorithm"><span class="nav-number">3.3.</span> <span class="nav-text">多实例——银行家算法(Banker’s Algorithm)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BC%AA%E4%BB%A3%E7%A0%81"><span class="nav-number">3.3.1.</span> <span class="nav-text">伪代码</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">SongyangJi</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">251</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">45</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">109</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/03/20/%E6%AD%BB%E9%94%81/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="死锁 | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          死锁
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-03-20 15:36:12" itemprop="dateCreated datePublished" datetime="2022-03-20T15:36:12+08:00">2022-03-20</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-09-22 15:44:56" itemprop="dateModified" datetime="2022-09-22T15:44:56+08:00">2022-09-22</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" itemprop="url" rel="index"><span itemprop="name">操作系统</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="死锁的基本概念"><a href="#死锁的基本概念" class="headerlink" title="死锁的基本概念"></a>死锁的基本概念</h1><h2 id="死锁与饥饿"><a href="#死锁与饥饿" class="headerlink" title="死锁与饥饿"></a>死锁与饥饿</h2><ul>
<li><strong>死锁</strong>：一组处于等待（阻塞）状态的进程，每一个进程持有其他进程所需要的资源，而又等待使用其他进程所拥有的资源，致使这组进程互相等待，均无法向前推进。另一种定义：当一组进程中每个进程都在等待一个事件，而这一事件只能由这一组进程的另一个进程引起时，这组进程处于死锁状态。</li>
<li><strong>饥饿</strong>：就绪进程长时间得不到调度是处于等待状态，而不是死锁中的互相等待。若信号量的等待队列按照LIFO或优先级管理，则可能导致饥饿。</li>
</ul>
<h2 id="死锁形成的条件"><a href="#死锁形成的条件" class="headerlink" title="死锁形成的条件"></a>死锁形成的条件</h2><p>如果以下四个条件同时满足，就会引起死锁：</p>
<ul>
<li>mutual exclusion互斥：至少一个资源要求互斥地共享；</li>
<li>hold and wait占有并等待：一个进程至少占有一个资源并等待另一资源，该资源为其它进程所占有；</li>
<li>no preemption非抢占：资源不能被抢占，只能进程完成任务后自动释放；</li>
<li>circular wait循环等待：互相等待形成一个环。</li>
</ul>
<h2 id="死锁的处理方法"><a href="#死锁的处理方法" class="headerlink" title="死锁的处理方法"></a>死锁的处理方法</h2><p>从大的方面来讲有四种解决方案：</p>
<ul>
<li><p><strong>使用协议预防死锁</strong><br>死锁预防是指一组方法，需要确定至少一个必要条件不成立。</p>
<p>预防的特点是<strong>保证一定不会死锁，但是会降低资源利用率和系统吞吐量</strong>，属于静态的处理方法。</p>
</li>
<li><p><strong>使用动态追踪技术避免死锁</strong></p>
<p>死锁避免要求操作系统事先得到有关进程申请使用资源的额外信息。当进程申请资源时，若发现满足该资源的请求可能导致死锁发生，则拒绝该申请。</p>
<p>死锁避免算法会因为<strong>追踪当前资源分配成本增加运行成本</strong>，但是<strong>相对于静态的死锁预防方法它允许更多的并发使用资源，所以系统吞吐量大于死锁预防</strong>。</p>
</li>
<li><p><strong>允许进入死锁状态，检测并加以恢复</strong></p>
</li>
<li><p><strong>忽视死锁问题</strong><br>大多数系统使用，因为死锁发生并不频繁，预防、避免和恢复耗费太大。</p>
</li>
</ul>
<h1 id="死锁预防"><a href="#死锁预防" class="headerlink" title="死锁预防"></a>死锁预防</h1><p>死锁预防是指实现破坏死锁的形成条件，预防的特点是一定不会死锁，但是会降低资源利用率和系统吞吐量。</p>
<h3 id="1-互斥"><a href="#1-互斥" class="headerlink" title="1. 互斥"></a>1. 互斥</h3><p>非共享资源必须互斥，共享资源不要求互斥所以不会死锁。无法从互斥条件下手避免死锁。</p>
<h3 id="2-占有并等待"><a href="#2-占有并等待" class="headerlink" title="2. 占有并等待"></a>2. 占有并等待</h3><ul>
<li><p>拥有不等待：资源静态分配策略，要求一个进程在执行前获得所有资源</p>
</li>
<li><p>等待不拥有：进程在申请其他资源的时候必须释放已分配的资源。</p>
</li>
</ul>
<p>缺点：资源利用率低，分配以后可能很久不被使用；产生饥饿，对于第一种协议，如果有进程需要多个常用资源，就可能会永久等待。</p>
<h3 id="3-非抢占"><a href="#3-非抢占" class="headerlink" title="3. 非抢占"></a>3. 非抢占</h3><p>如果一个进程占有一些资源并在申请一些无法立刻分配到的资源，那么它占有的这些资源就都可以被抢占。该进程将会在它重新获得原有的资源以及原本要申请的资源的时候重启。</p>
<p>用于状态可保存和恢复的资源如CPU，memory等，不适用于打印机等。</p>
<h3 id="4-循环等待"><a href="#4-循环等待" class="headerlink" title="4. 循环等待"></a>4. 循环等待</h3><p>对所有资源类型进行完全排序，要求每个进程按递增顺序申请资源。</p>
<p>如哲学家就餐问题，桌上的五根筷子分别编号，所有人只能按照从小到大的顺序申请筷子，所以永远不会出现每个人获得一只筷子的情况。</p>
<p>再比如，当一个进程对资源的使用顺序为5→2→4→3→1的时候，它的申请顺序仍然是12345，也就是说即使1是最后使用，也要先申请。这种浪费在程序越大、申请资源越多的时候越是明显。</p>
<h1 id="死锁避免"><a href="#死锁避免" class="headerlink" title="死锁避免"></a>死锁避免</h1><h2 id="安全状态-safe-state"><a href="#安全状态-safe-state" class="headerlink" title="安全状态(safe state)"></a>安全状态(safe state)</h2><p>如果系统能按照某个顺序为每个进程分配资源并能避免死锁，系统状态就是安全的。或者说，<strong>如果存在一个安全序列safe sequence&lt;P0,P1,P2,…,Pn&gt;，使得前面的进程能够得到足够的资源完成，同时它释放的资源又能满足后面的进程的话，就是安全的</strong>。</p>
<p>下面给出一个简单的例子：</p>
<table>
<thead>
<tr>
<th>process</th>
<th>maximum</th>
<th>allocation</th>
<th>need</th>
<th>available</th>
</tr>
</thead>
<tbody><tr>
<td>p0</td>
<td>10</td>
<td>5</td>
<td>5</td>
<td>3</td>
</tr>
<tr>
<td>p1</td>
<td>4</td>
<td>2</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>p2</td>
<td>9</td>
<td>2</td>
<td>7</td>
<td>3</td>
</tr>
</tbody></table>
<p>现在给出一个安全序列&lt;P1,P0,P2&gt;，之所以说它是安全序列，可以这样分析：P1分配到2个资源以后可以完成任务释放4个资源，那么总共可用资源变成5个，分配给P0以后待其完成任务释放10个资源可以让P2使用，这个分配顺序能确保不会产生死锁，所以系统状态是安全的。</p>
<p>但是，<strong>不安全状态并不一定会导致死锁</strong>。因为我们这里的最大资源需求量很有可能会超出真正需求量，反之，死锁状态一定是不安全状态。</p>
<p><strong>有了安全状态的概念，就可以定义避免算法以确保系统不会死锁，其思想就是简单的确保系统始终处于安全状态。</strong></p>
<h2 id="单实例——资源分配图-RAG——resource-allocation-graph"><a href="#单实例——资源分配图-RAG——resource-allocation-graph" class="headerlink" title="单实例——资源分配图(RAG——resource-allocation graph)"></a>单实例——资源分配图(RAG——resource-allocation graph)</h2><p>使用具有claim边的RAG，适用于每种资源类型有单个实例的资源分配系统。</p>
<p><img src="/2022/03/20/%E6%AD%BB%E9%94%81/rag.png"></p>
<p>有如下重要概念：</p>
<ol>
<li>虚边：将要请求或可能使用claim edge;</li>
<li>实边：请求边和分配边request，assignment</li>
</ol>
<p>规定：<br>如果没有环，就是安全状态；<br>如果有环，即使环中有虚边，也是不安全状态，在避免算法里面是不允许出现的；<br>如果出现了实边环，就是死锁。</p>
<h2 id="多实例——银行家算法-Banker’s-Algorithm"><a href="#多实例——银行家算法-Banker’s-Algorithm" class="headerlink" title="多实例——银行家算法(Banker’s Algorithm)"></a>多实例——银行家算法(Banker’s Algorithm)</h2><blockquote>
<p>在银行中，客户申请贷款的数量是有限的，每个客户在第一次申请贷款时要声明完成该项目所需的最大资金量，在满足所有贷款要求时，客户应及时归还。银行家在客户申请的贷款数量不超过自己拥有的最大值时，都应尽量满足客户的需要。在这样的描述中，银行家就好比操作系统，资金就是资源，客户就相当于要申请资源的进程。</p>
</blockquote>
<blockquote>
<p>上面的算法针对的是每个资源都只有单实例的情况。</p>
</blockquote>
<p>当新进程进入系统时，必须说明其可能需要的每种类型资源实例的最大数量。用户申请一组资源时，系统必须确保这些资源分配后系统仍处于安全状态。</p>
<p>设系统中共有n个进程和m种资源类型</p>
<ul>
<li><p>安全性算法：确定计算机系统是否处于安全状态</p>
<ul>
<li>向量finish[n]存储进程是否已经完成，初始状态为false；</li>
<li>向量available[m]存储当前每种资源的剩余可用量，初始值为资源总量；</li>
<li>向量need[n] [m]存储每个进程的所需要的每种资源的数量(need即max——最多需要多少资源)；</li>
<li>寻找是否存在finish&#x3D;false且所需资源need[i] ≤ available的进程，如果存在，让这个进程获得所需要的资源执行结束，然后释放资源，这个时候它的finish&#x3D;true，而总的available也要加上该进程原来占有的资源；</li>
<li>循环执行上一步直到没有符合条件的进程。这时候如果finish全部为true，那么系统处于安全状态，我们就能获得一个安全序列。</li>
</ul>
</li>
<li><p>资源请求算法：判断是否可安全允许请求</p>
<ul>
<li>确定request≤need，否则出错；request≤available，否则等待</li>
<li>按照请求假分配，修改系统当前的available、need，然后判断假分配以后系统状态是否安全。如果安全，该分配得到允许。</li>
</ul>
</li>
</ul>
<h3 id="伪代码"><a href="#伪代码" class="headerlink" title="伪代码"></a>伪代码</h3><p><strong>P - 进程的集合</strong><br><strong>Mp - 进程p的最大的请求数目</strong><br><strong>Cp - 进程p当前被分配的资源</strong><br><strong>A - 当前可用的资源</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> (P != ∅) &#123;</span><br><span class="line">    found = FALSE;</span><br><span class="line">    foreach (p ∈ P) &#123;</span><br><span class="line">        <span class="keyword">if</span> (Mp − Cp ≤ A) &#123;</span><br><span class="line">             <span class="comment">/* p可以獲得他所需的資源。假設他得到資源後執行；執行終止，並釋放所擁有的資源。*/</span></span><br><span class="line">             A = A + Cp ;</span><br><span class="line">             P = P − &#123;p&#125;;</span><br><span class="line">             found = TRUE;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (! found) <span class="keyword">return</span> FAIL;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">return</span> OK;</span><br></pre></td></tr></table></figure>





<blockquote>
<p><a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/%E9%93%B6%E8%A1%8C%E5%AE%B6%E7%AE%97%E6%B3%95">银行家算法</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/OS/" rel="tag"># OS</a>
              <a href="/tags/%E7%AE%97%E6%B3%95/" rel="tag"># 算法</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/03/20/%E4%B8%80%E4%BA%9B%E8%BF%98%E4%B8%8D%E9%94%99%E7%9A%84%E9%A2%98-3/" rel="prev" title="一些还不错的题-3">
                  <i class="fa fa-chevron-left"></i> 一些还不错的题-3
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/03/20/CDN/" rel="next" title="CDN">
                  CDN <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SongyangJi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  





</body>
</html>
